package daosupport;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class BaseDaoImpl {
	public DataSource dataSource;
	public Connection connection;
	private String proName;//procedure name
	public PreparedStatement preparedStatement;
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource){
		this.dataSource = dataSource;
	}
	public void setProNameProcedure(String proName) throws SQLException {
		this.proName = proName;
		connection = dataSource.getConnection();
		preparedStatement = connection
				.prepareStatement("call "+proName);
	}
	public void setProNameFuction(String proName) throws SQLException {
		this.proName = proName;
		connection = dataSource.getConnection();
		preparedStatement = connection
				.prepareStatement("select "+proName);
	}
	public void setIntParam(int index,int val) throws SQLException{
		preparedStatement.setInt(index, val);
	}
	public void setDoubleParam(int index,double val) throws SQLException{
		preparedStatement.setDouble(index, val);
	}
	public void setStringParam(int index,String val) throws SQLException{
		preparedStatement.setString(index, val);
	}
	public void setBoolean(int index, boolean val) throws SQLException{
		preparedStatement.setBoolean(index, val);
	}
	public  int excuteAsFunction() throws SQLException{
		System.out.println("----EXCUTE:"+proName);
		ResultSet resultSet=execFucntion();
		resultSet.next();
		int kq= resultSet.getInt(1);
		System.out.println("---kq="+kq);
		return kq;
	}
	public  long excuteAsFunctionForReport() throws SQLException{
		System.out.println("----EXCUTE:"+proName);
		ResultSet resultSet=execFucntion();
		resultSet.next();
		long kq= resultSet.getLong(1);
		System.out.println("---kq="+kq);
		return kq;
	}
	public synchronized ResultSet execFucntion(){
		try {
			return preparedStatement.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return null;
	}
	public ResultSet excuteAsProcedure() throws SQLException{
		System.out.println("----EXCUTE:"+proName);
		ResultSet resultSet=preparedStatement.executeQuery();
		return resultSet;
	}
	
}
